<template>
    <div class="login-wrap">
        <div class="ms-title">登录管理系统</div>
        <div class="ms-login">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="0px" class="demo-ruleForm">

                <el-form-item prop="name">
                    <el-input v-model="ruleForm.name" placeholder="账号"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" placeholder="密码" v-model="ruleForm.password"></el-input>
                </el-form-item>
                <el-form-item prop="validate">
                    <el-input v-model="ruleForm.validate" class="validate-code" placeholder="验证码"></el-input>
                    <div class="code" @click="refreshCode">
                        <s-identify :identifyCode="identifyCode"></s-identify>
                    </div>
                </el-form-item>
                <!-- <div v-if="errorInfo">
                    <span>{{errInfo}}</span>
                </div> -->
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                </div>
                <p class="register" @click="handleCommand()">注册</p>
            </el-form>
        </div>
    </div>
</template>

<script>
    export default {
        name: 'login',
        data() {
            return {
                // identifyCodes: "1234567890abcdefjhijklinopqrsduvwxyz", //验证码字典
                identifyCodes: "12345", //验证码字典
                identifyCode: "",
                // errorInfo: false,
                ruleForm: {
                    name: '',
                    password: '',
                    validate: ''
                },
                rules: {
                    name: [
                        // { required: true, message: '请输入用户名', trigger: 'blur' }
                    ],
                    password: [
                        // { required: true, message: '请输入密码', trigger: 'blur' }
                    ],
                    validate: [
                        // { required: true, message: '请输入验证码', trigger: 'blur' }
                    ]
                }
            }
        },
        // created(){  //在模板渲染成html前调用，即通常初始化某些属性值，然后再渲染成视图。
        //      console.log("怎么样啊hello");
        // },
        mounted() { //在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作。
            this.identifyCode = "";
            this.makeCode(this.identifyCodes, 4);
            // console.log("怎么样啊aaa");
        },
        methods: {
            submitForm(formName) {
                // debounceAjax(formName)
                const self = this;
                let names = self.ruleForm.name;
                let passwords = self.ruleForm.password;
                let validates = self.ruleForm.validate;
                if (names.length == 0) {
                    self.$message({
                        message: '请输入用户名！',
                        type: 'error',
                        // center: true
                    });
                    return false;
                } else {
                    if (passwords.length == 0) {
                        self.$message({
                            message: '请输入密码！',
                            type: 'error',
                            // center: true
                        });
                        return false;
                    } else {
                        if (validates.length == 0) {
                            self.$message({
                                message: '请输入验证码！',
                                type: 'error',
                                // center: true
                            });
                            return false;
                        } else if(self.ruleForm.validate.toLowerCase()==self.identifyCode.toLowerCase()){
                            self.$http.post('/api/user/login', JSON.stringify(self.ruleForm))
                                .then((response) => {
                                    if (response.data == -1) {
                                        self.$message({
                                            message: '该用户不存在...',
                                            type: 'error',
                                            // center: true
                                        });
                                    } else if (response.data == 0) {
                                        self.$message({
                                            message: '密码错误！',
                                            type: 'error',
                                            // center: true
                                        });
                                    } else if (response.status == 200) {
                                        self.$router.push('/readme');
                                        self.$message({
                                            message: '登录成功，欢迎回来！',
                                            type: 'success',
                                            center: true
                                        });
                                        sessionStorage.setItem('ms_username', self.ruleForm.name);
                                        sessionStorage.setItem('ms_user', JSON.stringify(self.ruleForm));
                                        console.log(JSON.stringify(self.ruleForm));
                                    }
                                }).then((error) => {
                                    console.log(error);
                                })
                        }else{
                            self.$message({
                                            message: '验证码错误！',
                                            type: 'error',
                                            // center: true
                                        });
                        }
                    }
                }
            },
            handleCommand() {
                this.$router.push('/register');
            },
            randomNum(min, max) {
                return Math.floor(Math.random() * (max - min) + min);
            },
            refreshCode() {
                this.identifyCode = "";
                this.makeCode(this.identifyCodes, 4);
            },
            makeCode(o, l) {
                for (let i = 0; i < l; i++) {
                    this.identifyCode += this.identifyCodes[
                        this.randomNum(0, this.identifyCodes.length)
                    ];
                }
                console.log(this.identifyCode);
            },
            // debounce(func, delay) {
            //     return function (args) {
            //         var _this = this
            //         var _args = args
            //         clearTimeout(func.id)
            //         func.id = setTimeout(function () {
            //             func.call(_this, _args)
            //         }, delay)
            //     }
            // },
            // submitDebounce(formName) {
            //     const self = this;
            //     self.$refs[formName].validate((valid) => {
            //         if (valid) {
            //             localStorage.setItem('ms_username', self.ruleForm.name);
            //             localStorage.setItem('ms_user', JSON.stringify(self.ruleForm));
            //             console.log(JSON.stringify(self.ruleForm));
            //             self.$http.post('/api/user/login', JSON.stringify(self.ruleForm))
            //                 .then((response) => {
            //                     console.log(response);
            //                     if (response.data == -1) {
            //                         self.errorInfo = true;
            //                         self.errInfo = '该用户不存在';
            //                         console.log('该用户不存在')
            //                     } else if (response.data == 0) {
            //                         console.log('密码错误')
            //                         self.errorInfo = true;
            //                         self.errInfo = '密码错误';
            //                     } else if (response.status == 200) {
            //                         self.$router.push('/readme');
            //                     }
            //                 }).then((error) => {
            //                     console.log(error);
            //                 })
            //         } else {
            //             console.log('error submit!!');
            //             return false;
            //         }
            //     });
            // },
            // debounceAjax() {
            //     debounce(submitDebounce, 1000);
            // }
        }
    }
</script>

<style>
    html,
    body {
        background-color: #324157;
        width: 100%;
        height: 100%;
        overflow: hidden;
    }

    .ms-title {
        margin-top: 5%;
        margin-bottom: 30px;
        text-align: center;
        font-size: 30px;
        color: #fff;
    }

    .ms-login {
        width: 300px;
        height: 240px;
        margin: 0 auto;
        padding: 40px;
        border-radius: 5px;
        background: #fff;
    }

    .ms-login span {
        color: #fff;
    }

    .login-btn {
        text-align: center;
    }

    .login-btn button {
        width: 100%;
        height: 36px;
    }

    .code {
        width: 112px;
        height: 35px;
        border: 1px solid #ccc;
        float: right;
        border-radius: 2px;
    }

    .validate-code {
        width: 136px;
        float: left;
    }

    .register {
        font-size: 14px;
        line-height: 30px;
        color: #999;
        cursor: pointer;
        float: right;
    }
</style>